🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / data / src / commonMain / kotlin / org / meshtastic / core / data / repository / BootloaderOtaQuirksRepositoryImpl.kt
Displaying Raw • Download
core/data/src/commonMain/kotlin/org/meshtastic/core/data/repository/BootloaderOtaQuirksRepositoryImpl.kt bd2863243bab6eb213401d949839a2bc74dde7e2 (bd286324) Text, 4.38 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.repository
Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.sync.Mutex
Tff7b72import T7ee787kotlinx.coroutines.sync.withLock
Tff7b72import T7ee787kotlinx.serialization.json.Json
Tff7b72import T7ee787org.koin.core.annotation.Single
Tff7b72import T7ee787org.meshtastic.core.common.util.safeCatching
Tff7b72import T7ee787org.meshtastic.core.data.datasource.BootloaderOtaQuirksLocalDataSource
Tff7b72import T7ee787org.meshtastic.core.data.datasource.BundledAssetReader
Tff7b72import T7ee787org.meshtastic.core.data.datasource.decode
Tff7b72import T7ee787org.meshtastic.core.database.entity.asEntity
Tff7b72import T7ee787org.meshtastic.core.database.entity.asExternalModel
Tff7b72import T7ee787org.meshtastic.core.di.CoroutineDispatchers
Tff7b72import T7ee787org.meshtastic.core.model.BootloaderOtaQuirksResponse
Tff7b72import T7ee787org.meshtastic.core.network.BootloaderOtaQuirksRemoteDataSource
Tff7b72import T7ee787org.meshtastic.core.repository.BootloaderOtaQuirksRepository
T8b949e/**
* Caches the nRF52 bootloader/OTA quirk catalog from the Meshtastic API (`/resource/bootloaderOtaQuirks`), seeded from
* the bundled `device_bootloader_ota_quirks.json` snapshot so it is never empty offline. Unlike
* [DeviceHardwareRepositoryImpl] (which this mirrors for seeding), [reconcile] carries no TTL of its own —
* [DeviceHardwareRepositoryImpl]'s own refresher calls it on the same cadence as its catalog refresh, so the two caches
* age together instead of drifting on separate schedules.
*/
Tf0883e@Single
Tff7b72class T56d364BootloaderOtaQuirksRepositoryImplTb4b4b4(
Tff7b72private Tff7b72val Te6edf3remoteDataSourceTb4b4b4: Te6edf3BootloaderOtaQuirksRemoteDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3localDataSourceTb4b4b4: Te6edf3BootloaderOtaQuirksLocalDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3assetReaderTb4b4b4: Te6edf3BundledAssetReaderTb4b4b4,
Tff7b72private Tff7b72val Te6edf3jsonTb4b4b4: Te6edf3JsonTb4b4b4,
Tff7b72private Tff7b72val Te6edf3dispatchersTb4b4b4: Te6edf3CoroutineDispatchersTb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3BootloaderOtaQuirksRepository Tb4b4b4{
T8b949e/** Serializes seeding and network writes so concurrent callers don't duplicate/interleave them. */
Tff7b72private Tff7b72val Te6edf3writeMutex Tff7b72= Te6edf3MutexTb4b4b4(Tb4b4b4)
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffgetSnapshotTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3BootloaderOtaQuirksResponse Tb4b4b4{
Te6edf3ensureSeededTb4b4b4(Tb4b4b4)
Tff7b72return Te6edf3localDataSourceTb4b4b4.Te6edf3getTb4b4b4(Tb4b4b4)Tff7b72?.Te6edf3asExternalModelTb4b4b4(Tb4b4b4) Tff7b72?: Te6edf3BootloaderOtaQuirksResponseTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffreconcileTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3safeCatching Tb4b4b4{ Te6edf3remoteDataSourceTb4b4b4.Te6edf3getBootloaderOtaQuirksTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3onSuccess Tb4b4b4{ Te6edf3response Tff7b72-Tff7b72> Te6edf3writeMutexTb4b4b4.Te6edf3withLock Tb4b4b4{ Te6edf3storeTb4b4b4(Te6edf3responseTb4b4b4) Tb4b4b4} Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3e Tff7b72-Tff7b72> Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffBootloaderOtaQuirksRepository: network refresh failedTa5d6ff" Tb4b4b4} Tb4b4b4}
Tb4b4b4}
T8b949e/** Seeds the cache from the bundled snapshot if empty (fresh install, data clear). */
Tff7b72private Tff7b72suspend Tff7b72fun Td2a8ffensureSeededTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3countTb4b4b4(Tb4b4b4) Tff7b72> T79c0ff0Tb4b4b4) Tff7b72return
Te6edf3writeMutexTb4b4b4.Te6edf3withLock Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3countTb4b4b4(Tb4b4b4) Tff7b72=Tff7b72= T79c0ff0Tb4b4b4) Tb4b4b4{
Te6edf3safeCatching Tb4b4b4{
Tff7b72val Te6edf3asset Tff7b72=
Te6edf3assetReaderTb4b4b4.Te6edf3decodeTff7b72<Te6edf3BootloaderOtaQuirksResponseTff7b72>Tb4b4b4(Ta5d6ff"Ta5d6ffdevice_bootloader_ota_quirks.jsonTa5d6ff"Tb4b4b4, Te6edf3jsonTb4b4b4)
Te6edf3assetTff7b72?.Te6edf3let Tb4b4b4{ Te6edf3storeTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4}
Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3e Tff7b72-Tff7b72>
Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffBootloaderOtaQuirksRepository: failed to seed from bundled JSONTa5d6ff" Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
T8b949e/**
* Not locked itself — every caller already holds [writeMutex] for the duration of a write. An entirely empty
* response (no devices AND no softDeviceVariants) is ignored rather than stored, so a bad or transient response can
* never wipe an existing seed/cache back to nothing.
*/
Tff7b72private Tff7b72suspend Tff7b72fun Td2a8ffstoreTb4b4b4(Te6edf3responseTb4b4b4: Te6edf3BootloaderOtaQuirksResponseTb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3responseTb4b4b4.Te6edf3devicesTb4b4b4.Te6edf3isEmptyTb4b4b4(Tb4b4b4) Tff7b72&Tff7b72& Te6edf3responseTb4b4b4.Te6edf3softDeviceVariantsTb4b4b4.Te6edf3isEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3w Tb4b4b4{ Ta5d6ff"Ta5d6ffBootloaderOtaQuirksRepository: empty response; leaving cache untouchedTa5d6ff" Tb4b4b4}
Tff7b72return
Tb4b4b4}
Te6edf3localDataSourceTb4b4b4.Te6edf3upsertTb4b4b4(Te6edf3responseTb4b4b4.Te6edf3asEntityTb4b4b4(Tb4b4b4)Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.05s